package com.ganbo.ms.io;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {

    /**
     * 每个Tick包大小
     */
    private static Integer binaryPacketSize = 285;

    /**
     * 使用RandomAccessFile进行文件拷贝
     *
     * @throws IOException
     */
    @Test
    public void t1() throws IOException {
        RandomAccessFile accessFile1 = new RandomAccessFile("d://mdc.data", "r");
        RandomAccessFile accessFile2 = new RandomAccessFile("d://mdc.data_back", "rw");

        byte[] tempSize = new byte[285];

        boolean loop = true;
        while (loop) {
            long syL = (accessFile1.length() - accessFile1.getFilePointer()) / binaryPacketSize;
            if (syL > 0) {
                accessFile1.read(tempSize);
                accessFile2.write(tempSize);
            } else loop = false;

        }

        accessFile1.close();
        accessFile2.close();
        System.out.println("文件拷贝完毕.....");
    }

    public static void main(String[] args) throws IOException {
        RandomAccessFile accessFile1 = new RandomAccessFile("d://mdc.data", "r");
        System.out.println(accessFile1.length() % binaryPacketSize);
    }
}
